/*
 * Residency.c
 * Behavioral Attributed Automaton
 * @author Gregoire JACOB (gregoire.jacob@orange-ftgroup.com)
 * @date 01/12/2008
 * @version 2.1
 * Residency parser automaton
 */

#include ".\Includes\BehaviorAutomata.h"


void initResidencyAutomata(){
	int start[3] = {0,0,0};
	long lstart[3] = {0,0,0};
	residency.automata = NULL;
	residency.nbnautomata = 0;
	residency.nbmaxautomata = 0;
	startAutomaton(&residency,'s',lstart,start);
}


void parseResWrite(long arg1id, int arg1type, long arg2id, int arg2type){
	char curnode;
	int start[3] = {0,0,0}; long lstart[3] = {0,0,0};
	int i, nb = residency.nbnautomata;

	for(i=0;i<nb;i++){
		struct PARSED_AUTOMATON * aut = &residency.automata[i];
		//Recover the state of the automaton
		curnode = getCurrentState(aut);
		switch(curnode){
			
			case 's': //Simple write
				//Checks semantic rules
				if(arg2type==TYPE_BOOT){
					//Create a waiting new automaton replacing the parsed one
					startAutomaton(&residency,'s',lstart,start);
					aut = &residency.automata[i]; //Update relocation
					//Progression toward the next node
					addNode(aut,'f',lstart,start);
					printf("[+] Residency has been detected "
						"on %s%d with value %s%d\n",typestr[arg2type],
						arg2id,typestr[arg1type],arg1id);
					storeBehaviorEntry(pf_BehDB,RESIDENCY_REF,0,arg1id,arg2id,0);
				}
				break;
		}//swithch
	}//for
}


void updateResidencyAutomata(unsigned long operation,
							   long arg1id, int arg1type,
							   long arg2id, int arg2type){
	switch(operation){
		case OP_WRITE:
			parseResWrite(arg1id,arg1type,arg2id,arg2type);
			break;
		//default:
			//ignore operation
	}

}